<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;

class Login extends Controller
{
    public function index()
    {
        echo 'this is dir '.__DIR__;
        return '<h1>hello Thinkphp</h1>';
    }

    public function login()
    {
        if($this->request->isPost()){
            $data = $this->request->post();
            $user_info = Db::name('SystemUser')->where('username', $data['username'])->find();
            if(!$user_info){
                $data = [
                    'code' => 0,
                    'msg'   => '用户不存在！！',
                ];
                return json($data);
            }
            if($user_info['password'] != md5($data['password'])){
                $data = [
                    'code' => 0,
                    'msg'   => '密码不正确！！',
                ];
                return json($data);
            }
            $user_info['login_num'] = $user_info['login_num']+1;
            $user_info['login_at'] = date('Y-m-d H:i:s');
            Db::name('SystemUser')->where('username', $data['username'])->update($user_info);
            $user_info['nodes'] = Db::name('SystemAuthNode')->whereIn('auth', $user_info['authorize'])->column('node');
            session('user', $user_info);
            $data = [
                'code' => 200,
                'msg'   => '登录成功',
            ];
            return json($data);
        } else {
            return view('login');
        }
    }

    public function logout() {
        session('user', null);
        session_destroy();
        $this->success('退出登录成功！！',url('admin/login/login'));
    }
}

